using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using Bamboo.Core.Data;
using Bamboo.Core.Common.Collection;


using TinyERP.ERP.Business;
using TinyERP.ERP.DTO;
using log4net;
using TinyERP.Core.View.Controls;

namespace TinyERP.View
{
    public class ProductListForm : BaseListForm
    {
        private ILog logger = LogManager.GetLogger(typeof(ProductListForm));
		private DataGridViewTextBoxColumn descriptionColumn;
		private DataGridViewTextBoxColumn nameColumn;
		private DataGridViewTextBoxColumn producerColumn;
		private DataGridViewTextBoxColumn maxUsageDateColumn;
        private DataGridViewTextBoxColumn measurementUnitGroupColumn;

		public IProductService ProductService 
		{
			get
			{
				return (IProductService) ServiceLocator.GetService("ProductServiceProxy");
			}
		}
		

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.descriptionColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nameColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.producerColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.maxUsageDateColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.measurementUnitGroupColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.bottomPanel.SuspendLayout();
            this.contentPanel.SuspendLayout();
            this.navigationPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // descriptionColumn
            // 
            this.descriptionColumn.Name = "descriptionColumn";
            // 
            // nameColumn
            // 
            this.nameColumn.Name = "nameColumn";
            // 
            // producerColumn
            // 
            this.producerColumn.Name = "producerColumn";
            // 
            // maxUsageDateColumn
            // 
            this.maxUsageDateColumn.Name = "maxUsageDateColumn";
            // 
            // measurementUnitGroupColumn
            // 
            this.measurementUnitGroupColumn.Name = "measurementUnitGroupColumn";
            // 
            // ProductListForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(617, 298);
            this.Name = "ProductListForm";
            this.bottomPanel.ResumeLayout(false);
            this.contentPanel.ResumeLayout(false);
            this.navigationPanel.ResumeLayout(false);
            this.navigationPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        
        
        private void SetupDataGridView()
        {
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
			this.descriptionColumn
			,
			this.nameColumn
			,
			this.producerColumn
			,
			this.maxUsageDateColumn
			,
			this.measurementUnitGroupColumn
            });
            
			this.descriptionColumn.DataPropertyName = "Description";
			this.descriptionColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Description, TinyERP.Core");
			this.descriptionColumn.Name = "descriptionColumn";
			this.descriptionColumn.Tag = "description";
			this.descriptionColumn.ReadOnly = true;
			this.descriptionColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.nameColumn.DataPropertyName = "Name";
			this.nameColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Name, TinyERP.Core");
			this.nameColumn.Name = "nameColumn";
			this.nameColumn.Tag = "name";
			this.nameColumn.ReadOnly = true;
			this.nameColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.producerColumn.DataPropertyName = "Producer";
			this.producerColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Producer, TinyERP.Core");
			this.producerColumn.Name = "producerColumn";
			this.producerColumn.Tag = "producer";
			this.producerColumn.ReadOnly = true;
			this.producerColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.maxUsageDateColumn.DataPropertyName = "MaxUsageDate";
			this.maxUsageDateColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("MaxUsageDate, TinyERP.Core");
			this.maxUsageDateColumn.Name = "maxUsageDateColumn";
			this.maxUsageDateColumn.Tag = "maxUsageDate";
			this.maxUsageDateColumn.ReadOnly = true;
			this.maxUsageDateColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.measurementUnitGroupColumn.DataPropertyName = "MeasurementUnitGroupName";
			this.measurementUnitGroupColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("MeasurementUnitGroup, TinyERP.Core");
			this.measurementUnitGroupColumn.Name = "measurementUnitGroupColumn";
			this.measurementUnitGroupColumn.Tag = "measurementUnitGroup";
			this.measurementUnitGroupColumn.ReadOnly = true;
			this.measurementUnitGroupColumn.SortMode = DataGridViewColumnSortMode.NotSortable;

            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.MultiSelect = false;
            this.dataGridView.ModuleAssemblyName = "TinyERP.Core";
            this.dataGridView.AllowUserToOrderColumns = true;
            this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            InitializeSortKeyMap();
        }
        
        private void InitializeSortKeyMap() 
        {
			this.dataGridView.SortKeyMap["MeasurementUnitGroupName"] = "MeasurementUnitGroup.Name";
        }

		public ProductListForm() : base()
        {
            InitializeComponent();
			SetupDataGridView();
        }
        

		public override void OnNew()
        {
            ProductEditInfoForm editForm = new ProductEditInfoForm();
            editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            };
            new DialogHelper(editForm).ShowDialog(UIManager.MainScreen);
        }
        

		public override void OnDelete()
        {
			if (this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            ProductDTO product = (ProductDTO)this.dataGridView.SelectedRows[0].DataBoundItem;
            try 
			{
                if (MessageBox.Show(CommonMessage.GetDeleteConfirmMessage("Product"),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
					ProductService.DeleteProductByID(product.ID);
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage("Product"));
					LoadGridData();
				}
			}
			catch(Exception ex) 
			{
				logger.Error(ex);
				MessageBox.Show(CommonMessage.GetCanNotDeleteError("Product"));
			}
        }
        
        
        public override void OnEdit()
        {
            if (this.dataGridView.SelectedRows == null || this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            ProductDTO productDTO = (ProductDTO) this.dataGridView.SelectedRows[0].DataBoundItem;
            ProductEditInfoForm editForm = new ProductEditInfoForm();
            editForm.IsEditMode = true;
            editForm.ApplyACL(this.AllowEdit, this.AllowDelete, this.AllowCreate);
            editForm.Product = productDTO;
			editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            };
            new DialogHelper(editForm).ShowDialog(UIManager.MainScreen);
        }
        

        

        
		protected override void LoadGridViewSearchData(IList<SearchCondition> searchConditions, Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
			long totalRecord = 0;
            IList productList = ProductService.SearchProduct(paging.CurrentPage, paging.PageSize, searchConditions, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            if (productList.Count == 0)
            {
                MessageBoxHelper.ShowInformation(AppResourceManager.GetInstance().GetString("ResultNotFound"));
            }
            SortableBindingList<ProductDTO> sortableProductList = SortableBindingList<ProductDTO>.CreateSortableBindingList(productList);
            this.dataGridView.DataSource = sortableProductList;
        }

        protected override void LoadGridViewDefaultData(Bamboo.Core.Data.SortDirection sortDirection, string columnName) 
        {
        	long totalRecord = 0;
            IList productList = ProductService.GetAllProduct(paging.CurrentPage, paging.PageSize, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            SortableBindingList<ProductDTO> sortableProductList = SortableBindingList<ProductDTO>.CreateSortableBindingList(productList);
            this.dataGridView.DataSource = sortableProductList;
        }        

    }
}
